package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.bean.PageRes;
import com.example.demo.bean.sysdict.SysDictDataDO;
import com.example.demo.bean.sysdict.SysDictTypeDO;
import com.example.demo.exception.OtherException;
import com.example.demo.mapper.sysdict.SysDictDataMapper;
import com.example.demo.mapper.sysdict.SysDictTypeMapper;
import com.example.demo.service.ISysDictTypeService;
import com.example.demo.dto.sysdict.SysDictTypePageReqVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictTypeDO> implements ISysDictTypeService {

    @Resource
    private SysDictTypeMapper sysDictTypeMapper;

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public PageRes<SysDictTypeDO> list(SysDictTypePageReqVO sysDictTypePageReqVO) {
        return sysDictTypeMapper.selectPage(sysDictTypePageReqVO);
    }


    @Override
    public int addSysDictType(SysDictTypeDO sysDictTypeDO) {
        validDictCodeExist(sysDictTypeDO.getDictCode(), null);
        return sysDictTypeMapper.insert(sysDictTypeDO);
    }

    @Override
    @Transactional
    public int deleteSysDictType(SysDictTypeDO sysDictTypeDO) {
        validDictTypeExist(sysDictTypeDO);
        // 查询数据字典
        SysDictTypeDO sysDictType = sysDictTypeMapper.selectById(sysDictTypeDO.getDictId());
        QueryWrapper<SysDictDataDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_type",sysDictType.getDictCode());
        // 删除数据字典和字典数据
        int deleteDictType = sysDictTypeMapper.deleteById(sysDictTypeDO.getDictId());
        int deleteDictData = sysDictDataMapper.delete(queryWrapper);
        return deleteDictData+deleteDictType;
    }

    @Override
    public int updateSysDictType(SysDictTypeDO sysDictTypeDO) {
        validDictTypeExist(sysDictTypeDO);
        validDictCodeExist(sysDictTypeDO.getDictCode(), sysDictTypeDO);
        return sysDictTypeMapper.updateById(sysDictTypeDO);
    }

    @Override
    public SysDictTypeDO getSysDictType(SysDictTypeDO sysDictTypeDO) {
        validDictTypeExist(sysDictTypeDO);
        return sysDictTypeMapper.selectById(sysDictTypeDO.getDictId());
    }

    private void validDictTypeExist(SysDictTypeDO sysDictTypeDO) {
        SysDictTypeDO exist = sysDictTypeMapper.selectById(sysDictTypeDO.getDictId());
        if (exist == null) {
            throw new OtherException("字典不存在");
        }
    }

    private void validDictCodeExist(String newDictCode, SysDictTypeDO sysDictTypeDO) {
        QueryWrapper<SysDictTypeDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code", newDictCode);
        SysDictTypeDO exist = sysDictTypeMapper.selectOne(queryWrapper);
        if (exist != null) {
            if (sysDictTypeDO != null) {
                if (!Objects.equals(exist.getDictId(), sysDictTypeDO.getDictId())) {
                    throw new OtherException("字典编码已存在");
                }
            } else {
                throw new OtherException("字典编码已存在");
            }
        }
    }
}
